from django import forms

from .models import Topic, Entry


class TopicForm(forms.ModelForm):
    class Meta:
        model = Topic
        fields = ['text']
        labels = {'text': ''} # 不要为字段text生成标签


class EntryForm(forms.ModelForm):
    class Meta:
        model = Entry
        fields = ['text', 'tag']
        labels = {'text': ''}
        widgets = {'text': forms.Textarea(attrs={'cols': 80})}

    def clean_tag(self):
        """对tag进行验证"""
        cleaned_data = self.cleaned_data['tag']
        if cleaned_data.isdigit():
            raise forms.ValidationError('tag不能是数字')
        return cleaned_data
